package com.gzhryc.shared_device.oem.code.place.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.place.dao.PlaceFreeMemberDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceFreeMember;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlaceFreeMemberSearch;

public class PlaceFreeMemberService {

	static Logger log = Logger.getLogger(PlaceFreeMemberService.class);

    public static PlaceFreeMemberService self() {
        return MultiDBTools.getService(PlaceFreeMemberService.class, true);
    }

    public static PlaceFreeMemberService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlaceFreeMemberService.class, true);
    }

    PlaceFreeMemberDao dao;

    public PlaceFreeMemberService(String jdbcKey) {
        this.dao = new PlaceFreeMemberDao(jdbcKey);
    }
    
    public List<PlaceFreeMember> findJoinBySearch(PlaceFreeMemberSearch search,Integer pageNo,Integer pageLimit){
    	Conditions condition = dao.buildConditions(search);
    	condition.and().likeBoth("place.name", search.getPlaceName());
    	condition.and().is("place.user_id", search.getPlaceUserId());
    	if(search.getActiveState() != null) {
    		if(search.getActiveState() == 1) {
    			condition.and().isNull("memberId");
    		}else {
    			condition.and().isNotNull("memberId");
    		}
    	}
    	condition.desc("createDate");
    	try {
			return dao.findJoinByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public Long countJoinBySearch(PlaceFreeMemberSearch search) {
    	Conditions condition = dao.buildConditions(search);
    	condition.and().likeBoth("place.name", search.getPlaceName());
      	condition.and().is("place.user_id", search.getPlaceUserId());
    	if(search.getActiveState() != null) {
    		if(search.getActiveState() == 1) {
    			condition.and().isNull("memberId");
    		}else {
    			condition.and().isNotNull("memberId");
    		}
    	}
    	try {
			return dao.countJoin(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return 0L;
    }
    
    public List<PlaceFreeMember> findJoinBySearch(PlaceFreeMemberSearch search){
    	Conditions condition = dao.buildConditions(search);
    	condition.and().likeBoth("place.name", search.getPlaceName());
    	if(search.getActiveState() != null) {
    		if(search.getActiveState() == 1) {
    			condition.and().isNull("memberId");
    		}else {
    			condition.and().isNotNull("memberId");
    		}
    	}
    	condition.desc("createDate");
    	try {
			return dao.findJoinByCondition(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public List<PlaceFreeMember> findByPlaceId(Long placeId){
    	if(placeId != null) {
    		Conditions condition = new Conditions(PlaceFreeMember.class);
    		condition.and().is("placeId", placeId);
    		
    		try {
				return this.dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public PlaceFreeMember getByKeys(Long placeId,String phone) {
    	if(placeId != null && StringTools.isNotBlank(phone)) {
    		Conditions condition = new Conditions(PlaceFreeMember.class);
    		condition.and().is("placeId", placeId);
    		condition.and().is("phone", phone);
    		
    		try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public boolean bindMember(Long placeId,String phone,Long memberId,String memberNickname) {
    	if(placeId != null && StringTools.isNotBlank(phone)) {
    		Conditions condition = new Conditions(PlaceFreeMember.class);
    		condition.and().is("placeId", placeId);
    		condition.and().is("phone", phone);
    		
    		PlaceFreeMember updateEntity = new PlaceFreeMember();
    		updateEntity.setMemberId(memberId);
    		updateEntity.setMemberNickname(memberNickname);
    		updateEntity.setActiveDate(new Date());
    		
    		try {
				return dao.updateNotNull(updateEntity,condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean add(PlaceFreeMember placeFreeMember) throws LogicException {
    	if(placeFreeMember.getPlaceId() != null) {
    		if(placeFreeMember.getPlace() == null) {
	    		PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeFreeMember.getPlaceId());
	    		if(place == null) {
	    			throw new LogicException("网点不存在");
	    		}
    		}
	    	placeFreeMember.setCreateDate(new Date());
	    	try {
				return dao.insert(placeFreeMember) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean updateNotNull(PlaceFreeMember placeFreeMember) {
    	if(placeFreeMember.getPlaceId() != null && StringTools.isNotBlank(placeFreeMember.getPhone())) {
    		Conditions condition = new Conditions(PlaceFreeMember.class);
    		condition.and().is("placeId", placeFreeMember.getPlaceId());
    		condition.and().is("phone", placeFreeMember.getPhone());
    		
    		try {
				return dao.updateNotNull(placeFreeMember,condition,"note") > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean deleteByKeys(Long placeId,String phone) throws LogicException{
    	if(placeId != null && StringTools.isNotBlank(phone)) {
    		Conditions condition = new Conditions(PlaceFreeMember.class);
    		condition.and().is("placeId", placeId);
    		condition.and().is("phone", phone);
    		try {
				return dao.deleteByCondition(condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }

}
